<?php


namespace App\Http\Controllers\App;

use App\Exceptions\FailException;
use App\Http\Common\Constant;
use App\Http\Controllers\Controller;
use App\Models\Member;
use App\Models\MemberCash;
use App\Models\MemberShareCash;
use Auth;
use Illuminate\Http\Request;

class MemberCashController extends Controller
{
    public function cash()
    {
        $member_id = Auth::id();

        $this->validate(request(), [
            'money' => 'required',
            'message' => 'required'
        ], [
            'money.required' => '提现金额不正确',
            'message.required' => '提现卡号'
        ]);

        $money = request('money');
        $message = request('message');

        if ($money < 1) {
            return $this->fail('金额超出范围');
        }

        $member = Member::where("id", $member_id)->first();

        if ($member->useable_bonus_money < $money) {
            return $this->fail('金额超出范围');
        }

        try{
            \DB::beginTransaction();

            $member->useable_bonus_money -=$money;
            $member->save();

            $member_cash = new MemberCash();
            $member_cash->member_id = $member_id;
            $member_cash->money = $money;
            $member_cash->proportion = Constant::MEMBER_CASH_PROPORTION;
            $member_cash->status = 1;
            $member_cash->message = $message;
            $member_cash->save();

            \DB::commit();
            return success('提现申请成功');

        }catch (\Exception $exception){
            \Log::error($exception);
            \DB::rollBack();
            throw new FailException("保存失败");
        }
    }

    // 分享提现C
    public function shareCash()
    {
        $member_id = Auth::id();

        $this->validate(request(), [
            'money' => 'required',
            'message' => 'required'
        ], [
            'money.required' => '提现金额不正确',
            'message.required' => '提现卡号'
        ]);

        $money = request('money');
        $message = request('message');

        if ($money < 1) {
            return $this->fail('金额超出范围');
        }

        $member = Member::where("id", $member_id)->first();

        if ($member->useable_share_money < $money) {
            return $this->fail('金额超出范围');
        }

        try{
            \DB::beginTransaction();
            $member->useable_share_money -=$money;
            $member->save();
            $member_cash = new MemberShareCash();
            $member_cash->member_id = $member_id;
            $member_cash->money = $money;
            $member_cash->status = 1;
            $member_cash->message = $message;
            $member_cash->save();

            \DB::commit();
            return success('提现申请成功');

        }catch (\Exception $exception){
            \Log::error($exception);
            \DB::rollBack();
            throw new FailException("保存失败");
        }
    }

    // 会员分享提现列表
    public function shareCashList(Request $request)
    {
        $member_id = Auth::id();
        $member_share_cash = MemberShareCash::where('member_id', $member_id)->orderBy('id','desc')->paginate($request->get('limit',15))->toArray();

        return [
            'count' => $member_share_cash['total'],
            'list'  => $member_share_cash['data']
        ];
    }

    // 会员分享提现列表
    public function CashList(Request $request)
    {
        $member_id = Auth::id();
        $member_cash = MemberCash::where('member_id', $member_id)->orderBy('id','desc')->paginate($request->get('limit',15))->toArray();

        return [
            'count' => $member_cash['total'],
            'list'  => $member_cash['data']
        ];
    }


}